package com.rest.demo.io;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator
 * @decription
 * @Date 2018-02-05 10:36
 */
public class CreateFileDemo {

    public static void main(String[] args) {
        //创建文件
        File f = new File("F:\\a2.txt");
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //File中的常量
        System.out.println("---\\---"+File.separator);
        System.out.println("----:---"+File.pathSeparator);

        String fileName = "F:"+File.separator+"a3.txt";
        File f2 = new File(fileName);
        try {
            f2.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //删除文件
        f.delete();
        f2.delete();

        //创建一个文件夹
        String directoryName = "F:"+File.separator+"20180215";
        System.out.println(directoryName);
        File f3 = new File(directoryName);
        f3.mkdir();

        //列出一个文件夹下所有的文件
        String directoryName2 = "F:\\guns-framework";
        File f4 = new File(directoryName2);
        List<File> fileNameList = Arrays.asList(f4.listFiles());
        fileNameList.stream().forEach(System.out::println);

        System.out.println("-----------------------------------------------");

        //递归打印一个文件夹下的所有文件
        String directoryName3 = "D:\\Documents";
        File f5 = new File(directoryName3);
        printAllFiles(f5);
    }

    /**
     * 递归打印所有文件
     */
    public static void printAllFiles(File f) {
        if(f.isDirectory()) {
            List<File> listFiles = Arrays.asList(f.listFiles());
            if(!listFiles.isEmpty()) {
                listFiles.stream().forEach(file -> printAllFiles(file));
            }
        } else {
                System.out.println(f);
        }
    }
}
